package com.ruoyi.web.controller.persion.controller;

import com.ruoyi.web.controller.persion.entity.Favorite ;
import com.ruoyi.web.controller.persion.service.IFavoriteService ;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/account/favorite")
public class FavoriteController extends BaseController {
    @Qualifier("favoriteServiceImpl")
    @Autowired
    private IFavoriteService favoriteService;

    @GetMapping("/list")
    public TableDataInfo list(Favorite favorite) {
        Long userId = SecurityUtils.getUserId();
        favorite.setUserId(userId);
        startPage();
        List<Favorite> list = favoriteService.selectFavoriteList();
        return getDataTable(list);
    }

    @PostMapping("/toggle")
    public AjaxResult toggleFavorite(@RequestParam Long seedId) {
        Long userId = SecurityUtils.getUserId();
        boolean isFavorite = favoriteService.toggleFavorite(userId, seedId);
        return success(isFavorite ? "收藏成功" : "已取消收藏");
    }

    @GetMapping("/check")
    public AjaxResult checkFavorite(@RequestParam Long seedId) {
        Long userId = SecurityUtils.getUserId();
        boolean isFavorite = favoriteService.isFavorite(userId, seedId);
        return success(isFavorite);
    }

    @GetMapping("/userFavorites")
    public AjaxResult getUserFavorites() {
        Long userId = SecurityUtils.getUserId();
        List<Favorite> favorites = favoriteService.getUserFavorites(userId);
        return success(favorites);
    }

    @DeleteMapping("/{favoriteIds}")
    public AjaxResult remove(@PathVariable Long[] favoriteIds) {
        return toAjax(favoriteService.deleteFavoriteByIds(favoriteIds));
    }
}